package com.example.learnelementuiandspringboot.controller;

import com.example.learnelementuiandspringboot.entity.Book;
import com.example.learnelementuiandspringboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.crypto.interfaces.PBEKey;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 国龙
 * @since 2024-02-02
 */
@RestController
@RequestMapping("/book")
@CrossOrigin(origins = "*",maxAge = 3600)//解决前端axios请求跨域问题  https://www.yuucn.com/a/1559410.html
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/list")    //http://localhost:8081/book/list
    public List<Book> list(){
        return this.bookService.list();
    }

    @GetMapping("/findById/{id}")
    public Book findById(@PathVariable("id") Integer id){

        return this.bookService.getById(id);
    }

    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        return this.bookService.removeById(id);
    }

    @PostMapping("/add")
    public boolean add(@RequestBody Book book){
        return this.bookService.save(book);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody Book book){
        return this.bookService.updateById(book);
    }
}
